package com.atguigu.mapreduce02;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;

import java.io.IOException;
import java.util.List;

/**
 * @author lvbingbing
 * @date 2020-07-26 21:06
 */
public class ReadFruitHDFSMapper extends TableMapper<Text, Text> {

    private Text k = new Text();

    private Text v = new Text();

    private StringBuilder stringBuilder = new StringBuilder();

    @Override
    protected void map(ImmutableBytesWritable key, Result result, Context context) throws IOException, InterruptedException {

        String rowKey = Bytes.toString(key.get());

        k.set(rowKey);

        List<Cell> cells = result.listCells();

        stringBuilder.setLength(0);

        for (Cell cell : cells) {
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            stringBuilder.append(value).append('\t');
        }

        v.set(stringBuilder.toString());
        context.write(k, v);
    }
}
